package com.xxx.coll;

import java.util.Iterator;
import java.util.TreeSet;

/**
 * @version v1.0
 * @Description:
 * @Author zhanzhongxin
 * @Date 2021/7/21 9:37
 * @Copyright 博为峰
 */
public class TreeSetDemo {
    public static void main(String[] args) {
        /*
           TreeSet: 有序，不同步
           1.自定义类实现Comparable接口，重写compareTo()方法，让对象自身具有排序（唯一性）
           2.自定义类实现Comparator接口，重写compare()方法，在创建TreeSet集合对象时把比较器类作为
             参数传入，让集合自身具有排序功能
         */

//        m1();
        TreeSet treeSet = new TreeSet(new ComparatorImpl());
        treeSet.add(new Worker("zhangsan",20));
        treeSet.add(new Worker("lisi",30));
        treeSet.add(new Worker("wangwu",50));
        treeSet.add(new Worker("wangwu",50));
        treeSet.add(new Worker("zhaoliu",60));
//        System.out.println(treeSet);
        Iterator iterator = treeSet.iterator();
        while (iterator.hasNext()){
            Worker worker = (Worker) iterator.next();
            System.out.println(worker.getName());
        }
    }

    public static void m1() {
        TreeSet treeSet = new TreeSet();
        treeSet.add(new Worker("zhangsan",20));
        treeSet.add(new Worker("lisi",30));
        treeSet.add(new Worker("wangwu",50));
        treeSet.add(new Worker("wangwu",50));
        treeSet.add(new Worker("zhaoliu",60));
//        System.out.println(treeSet);
        Iterator iterator = treeSet.iterator();
        while (iterator.hasNext()){
            Worker worker = (Worker) iterator.next();
            System.out.println(worker.getName());
        }
    }
}
